import Vue from 'vue'
import VueRouter from 'vue-router'
// import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/free',
    name: 'free',
    component: () => import(/* webpackChunkName: "about" */ '../views/Free.vue')
  },
  {
    path: '/goods',
    name: 'goods',
    component: () => import(/* webpackChunkName: "about" */ '../views/Goods.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: () => import(/* webpackChunkName: "about" */ '../views/User.vue'),
    redirect: 'cart',
    children: [
      {
        path: '/cart',
        name: 'cart',
        component: () => import(/* webpackChunkName: "about" */ '../components/user/Cart.vue'),
      }
    ]
    // beforeEnter: (to, from, next) => {
    //   let token = localStorage.getItem('x-auth-token')
    //   if (token) {
    //     next()
    //   } else {
    //     alert('请先登录')
    //   }

    // }
  },
  {
    path: '/order',
    name: 'order',
    component: () => import(/* webpackChunkName: "about" */ '../views/Order.vue')
  }
  ,
  {
    path: '/home',
    name: 'home',
    component: () => import(/* webpackChunkName: "about" */ '../views/Home.vue')
  },
  {
    path: '/details',
    name: 'details',
    component: () => import(/* webpackChunkName: "about" */ '../views/Details.vue')
  },
  {
    path: '*',
    name: 'error',
    component: () => import(/* webpackChunkName: "about" */ '../components/Error.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 导航守卫的三种模式
// 第一种全局前置守卫 beforeEach
// 第二种路由独享守卫 beforeEnter
// 第三种组内件守卫 beforeRouteEnter
// to: Route: 即将要进入的目标 路由对象
// from: Route: 当前导航正要离开的路由
// next: Function: 一定要调用该方法来 resolve 这个钩子。
// router.beforeEach((to, from, next) => {
//   console.log('beforeEach', to, from);
//   if (to.path == '/user') {
//     let token = localStorage.getItem('x-auth-token')
//     if (token) {
//       next()
//     } else {
//       alert('请先登录')
//     }
//   } else {
//     next()
//   }

// })
export default router
